package com.server;

import com.model.Student;
import com.model.User;

import java.io.*;
import java.util.List;
import java.util.ListIterator;

/**
 * 服务器端的学员系统操作
 */
public class StudentSystemOperation {
    private List<Student> students;

    public StudentSystemOperation() {
        serverReadStudentFile();
    }


    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    /**
     * 服务器端读取本地学员信息文件
     */
    public void serverReadStudentFile(){
        File file = new File("./ExamSystemServer/src/com/students.txt");
        FileInputStream fileInputStream = null;
        ObjectInputStream objectInputStream = null;
        try {
            // 读取服务器上的文件
            fileInputStream = new FileInputStream(file);
            objectInputStream = new ObjectInputStream(fileInputStream);
            List<Student> students = (List<Student>) objectInputStream.readObject();
            this.setStudents(students);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (objectInputStream != null){
                try {
                    objectInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStream != null){
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 服务器端将学员信息重新写入到本地文件
     * @param students
     */
    public void serverSaveStudentFile(List<Student> students){
        File file = new File("./ExamSystemServer/src/com/students.txt");
        FileOutputStream fileOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            // 写入服务器本地中
            fileOutputStream = new FileOutputStream(file);
            objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(students);
            objectOutputStream.flush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (objectOutputStream != null){
                try {
                    objectOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }if (fileOutputStream != null){
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 服务器端接收客户端发来的增加学员请求，并增加学员信息，返回是否添加成功
     * @param student
     * @return
     */
    public boolean add(Student student){
        // 当系统中无学员信息时，直接添加
        if (students.size() == 0){
            students.add(student);
        }
        // 若有学员信息时，则需要通过遍历所有学员信息的id，判断当前添加学员是否已经存在
        else {
            // 利用迭代器遍历，否则会出错
            ListIterator<Student> listIterator = students.listIterator();
            while (listIterator.hasNext()){
                Student cur_student = listIterator.next();
                // 判断添加学员id是否与系统中已有学员id相同,若相同，则提示已经存在，无需添加
                if (cur_student.getId().equals(student.getId())){
                    return false;
                }
            }
            // 若系统中无相同学员id，则遍历后再添加
            students.add(student);
        }
        // 保存到本地文件中
        serverSaveStudentFile(students);
        return true;
    }

    /**
     * 服务器端删除学员信息，并返回是否删除成功
     * @param id
     * @return
     */
    public boolean delete(String id){
        // 遍历学员信息，删除id的学员
        ListIterator<Student> listIterator = students.listIterator();
        while (listIterator.hasNext()){
            Student cur_student = listIterator.next();
            if (cur_student.getId().equals(id)){
                listIterator.remove();
                //System.out.println("当前id=" + id + "的学员已经被删除！");
                serverSaveStudentFile(students);
                return true;
            }
        }
        //System.out.println("输入学员id有误，系统中无该学员！");
        return false;
    }

    /**
     * 服务器端修改学员信息，默认id不能被修改，并返回是否修改成功
     * @param student
     * @return
     */
    public boolean modify(Student student){
        // 遍历学员信息，修改对应id的学员信息
        ListIterator<Student> listIterator = students.listIterator();
        while (listIterator.hasNext()){
            Student cur_student = listIterator.next();
            if (cur_student.getId().equals(student.getId())){
                cur_student.setUserName(student.getUserName());
                cur_student.setPassWord(student.getPassWord());
                cur_student.setAge(student.getAge());
                //System.out.println("学员信息修改成功！");
                serverSaveStudentFile(students);
                return true;
            }
        }
        return false;
    }

    /**
     * 服务器端修改学员信息，只修改密码
     * @param user
     * @return
     */
    public boolean modify(User user){
        // 遍历学员信息，修改对应id的学员信息
        ListIterator<Student> listIterator = students.listIterator();
        while (listIterator.hasNext()){
            Student cur_student = listIterator.next();
            if (cur_student.getUserName().equals(user.getUserName())){
                cur_student.setPassWord(user.getPassWord());
                serverSaveStudentFile(students);
                return true;
            }
        }
        return false;
    }

    /**
     * 服务器端查找学员信息，根据id查找，若存在返回该学生，否则返回null
     * @param id
     * @return
     */
    public Student find(String id){
        ListIterator<Student> listIterator = students.listIterator();
        while (listIterator.hasNext()){
            Student cur_student = listIterator.next();
            if (cur_student.getId().equals(id)){
                //System.out.println(cur_student.toString());
                return cur_student;
            }
        }
        return null;
    }

    /**
     * 服务器端返回所有学员，若无则返回null
     * @return
     */
    public List<Student> showAll(){
        if (students.size() == 0){
            return null;
        }
        else {
            return getStudents();
        }
    }
}
